<?php

class cGroup 
{
	public $showInAdmin = false;
	public $translated = array("name" => "-nevyplněno-");
	public $cms;

	function __construct(cCms $cms) {
		$this->cms=$cms;
		$this->defaultView = new cView(array("id"=>1),$cms->defaultView);
	}
	
	function getTranslated($what) {
		return $this->translated[$what];
	}

	
	
	function getName() {
		return strToLower(substr(get_Class($this),1));
	}	

	function getHotlinks() 
	{
		$hls=$this->listHotlinks();
		$s.="<h4 class=\"title\" onclick=\"toggleBox(this);\" >Hotlinky</h4><ul>";
		
		foreach ($hls as $key=>$hl) {
			$s.="<li><a href=\"".$this->defaultView->getUrl(array("group"=>$this->getName(),"content"=>"browser","browse"=>"hotlinks","hotlink"=>$key,"id"=>""))."\">".$hl."</a></li>";
		}
		return $s."</ul>";
	}
	
	function getHLSelect($id) 
	{
		$hl['hotlinks not implemented']="* LIMIT 0";
		return $hl[$id];
	}

	function handleRequest($view)
	{
		if ($view->content=="browser") { return $this->getBrowser($view); }
		if ($view->content=="tree") { return $this->getTree($view); }
		if ($view->content=="hotlinks") { return $this->getHotlinks($view); }
		//if ($view->content=="all") { return $this->getAll($view); }
		throw new Exception("Nevím, co chceš poslat!");
	}
	
	/*
	function getSide($req)
	{
		$dom = new DOMDocument;
		$dom->encoding="utf-8";
		$dom->appendChild($dom->createElement("hotlinks",$this->getHotlinks($req)));	
		$dom->appendChild($dom->createElement("tree",$this->getTree($req)));	
		$dom->appendChild($dom->createElement("browser",$this->getBrowser($req)));
		return $dom->saveXML();
	}
	*/


	function handlePost($post, cView $view)
	{
		try {
			//print_r($post);
			if (!is_numeric($post["id"])) {
				$node=$this->cms->file->loadNode($post['standard_treeitem']['group'],$post['standard_treeitem']['type']);
				$node = new $node($this->cms);
			} else {
				$node = $this->cms->tree->getNodeById($post["id"], $view); 
			}
			$node->handlePost($post, $view);
		} catch (Exception $e) {
			throw $e;
		} 
	}
	
	function listTree($view) {
		$rows = $this->cms->tree->selectRaw("type='folder' ORDER BY lft", $view);
		return $rows;
	}

	function getTree($view) 
	{
		$rows=$this->listTree($view);
		$tree = $this->cms->tree->levelizeRows($rows);
		
		$s.= '<h4 class="title" onclick="toggleBox(this); return(false);">Stromeček</h4><ul>';
		
		$lastLevel = 0;
		foreach ($tree as $i=>$n) {
			while($lastLevel>$n["level"]) { $s.="</ul></li>"; $lastLevel--; }
			if ($n["id"]==$view->id) {
				$sel = "class=\"nojs_active\"";
			} else $sel="";
			$s.="<li><a $sel href=\"".$this->makeLink(array("action"=>"show","id"=>$n["id"]))."\" ><span>".$n["name"]."</span></a>";
			if (($tree[($i+1)]["level"]<=$n["level"])==$n["rgt"]) { $s.="</li>\n"; } else $s.="<ul>";
			if ($i==count($tree) && $i>1) 
			{
				 $s.="</ul></li>"; 
			}
			$lastLevel = $n["level"];
		}		
		$s.="</ul>";
		return $s;	
	}
	
	function makeLink($args) 
	{
		$required=array("group"=>$this->getName(),"page"=>1,"action"=>"show","content"=>"browser","id"=>$this->defaultView->id);
		foreach ($required as $k=>$v) {
			if (!isset($args[$k])) {
				$get[]=$k."=".$v;
			} else $get[]=$k."=".$args[$k];
		}
		foreach ($args as $k=>$v) {
			if (!isset($required[$k])) {
				$get[]=$k."=".$args[$k];
			} 
		}
		return "?".implode("&",$get);
	}

	function listHotlinks()
	{
		return $this->hotlinks;
	}
	
	function getBrowser($view) 
	{
		
		if ($view->browse=="hotlinks" /*&& !is_numeric($view->id)*/) 
		{
			return $this->browseHL($view);
		}
		else if (is_numeric($view->id)) 
		{
			$node = $this->cms->tree->getNodeById($view->id, $view);
			return $node->view($view);
		} 
		else if (($view->type!=null) && ($view->group!=null)) 
		{
			$cName=$this->cms->file->loadNode($view->group,$view->type);

			$node = new $cName($this->cms);

			$sxml = $node->getDefaultData();
			$sxml->standard_treeitem->parent_id = $_POST['parent_id'];
			$sxml->standard_treeitem->group = $_POST['group'];
			$sxml->standard_treeitem->type = $_POST['type'];
			 
			$node->setData($sxml);
		
			return $node->view($view); 
		}
	}

	function browseHL(cView $view) 
	{
		$folder = $this->cms->tree->getRoot();
		
		$folder->setCustom('where',$s = $this->hlSQL[$view->hotlink]);

		//echo pes;
		$folder->setCustom('relation','subnodes');
		$folder->setStandard('name',$this->hotlinks[$view->hotlink]);
		return $folder->view($view); 
	}
	
}

?>